# 发布帖子
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Common.Utils import ChatDriver
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

class ForumPost:
    url = ""
    driver = ""

    def __init__(self):
        self.driver = ChatDriver.driver

    def clear_input_fields(self):
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").clear()
        # 定位元素
        element = self.driver.find_element(By.CSS_SELECTOR, "#edit-article > div:nth-child(2) > div.CodeMirror-scroll")

        # 创建 ActionChains 对象
        actions = ActionChains(self.driver)
        actions.move_to_element(element)  # 移动到元素位置
        actions.click()  # 点击元素使其聚焦
        # 模拟按下 Ctrl + A 全选内容
        actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL)
        # 模拟按下 Delete 键删除内容
        actions.send_keys(Keys.DELETE)
        # 执行操作
        actions.perform()

    def fill_post_and_submit(self, title, content):
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title").send_keys(title)
        # 定位目标元素
        element = self.driver.find_element(By.CSS_SELECTOR, "#edit-article > div:nth-child(2) > div.CodeMirror-scroll")
        # 模拟键盘事件
        actions = ActionChains(self.driver)
        actions.move_to_element(element)  # 移动到元素位置
        actions.click()  # 点击元素使其聚焦
        actions.send_keys(content)  # 发送内容
        actions.perform()  # 执行所有操作

        time.sleep(0.5)
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit").click()
        time.sleep(0.5)
        # 添加屏幕截图
        ChatDriver.getScreenShort()
        # ChatDriver.get_alert_image()

    # 发布帖子元素的检查
    def ForumPostBy(self):
        # 分别查找板块选择框、标题输入框、内容输入框、发布按钮
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_borad")
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_title")
        self.driver.find_element(By.CSS_SELECTOR, "#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll")
        self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit")

    # 发布帖子成功
    def ForumPostSuc(self):
        self.clear_input_fields()
        self.fill_post_and_submit("测试22222", "测试测试测试测试测试测试")
        WebDriverWait(self.driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col")))
        # 添加屏幕截图
        ChatDriver.getScreenShort()

    # 发布帖子失败
    def ForumPostFail(self):
        # 点击发布帖子
        self.driver.find_element(By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        # 定位元素，防止发布帖子按钮找不到
        element = self.driver.find_element(By.CSS_SELECTOR, "#article_post_submit")
        # 执行 JavaScript 代码，使元素底部与窗口底部对齐
        self.driver.execute_script("arguments[0].scrollIntoView(false);", element)
        # 等待一段时间，确保页面滚动完成
        time.sleep(1)

        test_cases = [
            {"title": "测试1", "content": ""},
            {"title": "", "content": "测试测试测试测试测试测试"},
            {"title": "", "content": ""}
        ]

        # 遍历执行所有测试用例
        for case in test_cases:
            # 清空输入框
            self.clear_input_fields()
            # 填写表单并提交
            self.fill_post_and_submit(
                title=case["title"],
                content=case["content"],
            )
